package cn.wh.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static void main(String[] args) {
        // System.out.println(System.currentTimeMillis());
        // System.out.println(getCurrentTime(null));
        // 当天开始时间
        System.out.println(getDayStartTime(null));
        // 当天结束时间
        System.out.println(getDayEndTime(null));
    }

    /**
     * 获取当前时间 默认yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDate(Date date ,String pattern){
        if(Utils.isNotEmpty(pattern)){
            try{
                SimpleDateFormat sdf1 = new SimpleDateFormat(pattern);
                return  sdf1.format(date);
            }catch (Exception e){
                return sdf.format(date);
            }
        }else{
            return sdf.format(date);
        }
    }

    /**
     * 获取当前时间 默认yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurrentTime(String pattern){
        if(Utils.isNotEmpty(pattern)){
            return formatDate(new Date(),pattern);
        }else{
            return sdf.format(new Date());
        }
    }

    /**
     * 获取当天起始时间
     * @return
     */
    public static Date getDayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }
    /**
     * 获取当天起始时间
     * @return
     */
    public static String getDayStartTime(String pattern) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return formatDate(todayStart.getTime(),pattern);
    }

    /**
     * 获取当天结束时间
     * @return
     */
    public static Date getDayEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }
    /**
     * 获取当天结束时间
     * @return
     */
    public static String getDayEndTime(String pattern) {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return formatDate(todayEnd.getTime(),pattern);
    }



}
